package show.me.the.money.resource;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;


public class ScriptFile extends ResourceFile{
	private char[] buf;
	private String encoding = "Cp1252";
	File file;
	public void setFileName(String fileName){
		file = new File(fileName);
		exists = file.exists();
	}
	public void load() throws IOException{
		if(exists && file.lastModified() != lastModi){
			lastModi = file.lastModified();
			InputStreamReader in=new InputStreamReader(new FileInputStream(file),encoding);
			int len=(int)file.length();
			buf = new char[len];
			in.read(buf);
			in.close();
		}		
	}

	public int length(){
		if(buf == null){
			return 0;
		}
		return buf.length;
	}
	
	public boolean exists(){
		return exists;
	}
	
	public String toString(){
		return new String(buf);
	}
	
	public InputStream getInputStream(){
		return null;
	}
	
	public void writeToStream(OutputStream outs,boolean needClose) throws IOException{
	   if(buf != null){
		    OutputStreamWriter out = new OutputStreamWriter(outs,encoding);
	    	out.write(buf,0,buf.length);
	    	out.write("\n");
	    	out.flush();
	    	if(needClose){
	    		out.close();
	    	}
	    }
	}
}
